<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>definepropety</title>
    <script>
        function observer (data){
            Object.keys(data).forEach(function (item){
                definedReactive (data,item,data[item])
            })
        }
        // 转 getter setter
        function definedReactive (obj,key,value){
            Object.defineProperty(obj,key,{
                get(){
                    return value;
                },
                set(newValue){
                    value = newValue;
                    title.innerHTML = newValue;
                }
            })
        }   
    </script>
</head>
<body>
    <div id="app">
        <button id="btn">设置</button>
        <p id="title">这是一个测试</p>

    </div>
    <script>
        let title = document.querySelector('#title');
        let btn = document.querySelector('#btn');
        let data = {
            title:'Hello , 测试',
        }
        observer(data);
        // 劫持 


        btn.onclick = function(){
            data.title = '改变了';
        }
    </script>
</body>
</html>